/**
 * 大数据按系统统计
 * @param startTime
 * @param endTime
 * @param systemType
 */
$(function(){
	bySystem("1");
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}; 
	//每次点击的时候清空表格
	$('tbody > tr td').remove();
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
			}else{
				if(page==1){
					bySystem(page);
				}else if(page>showPageN-4){
				}else{
					bySystem(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
});

/**
 * 获取后台传过来的page
 */
function  getPage(page){
	//每次点击的时候清空表格
	$('tbody > tr td').remove();
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var systemType = $("#systemType").val();
	var param = {
		"startTime" : startTime,
		"endTime" : endTime,
		"systemType" : systemType,
		"page" : page
	}
	var aj = $.ajax({
		url : '/web-adv/bySystem',
		data : param,
		type : 'post',
		success : function(data) {
			$('tbody > tr td').remove();
			$("#endTime").val(data.endTime);
			$("#startTime").val(data.startTime);
			var beans = data.bigData;
			var sumAdpayNum=0;
			var budgetMoney=0;
			var adPayNum=0;
			var proportionNum=0;
			for(var j = 0; j < beans.length;j++){
				if(typeof beans[j].clickNum=="object"){
					beans[j].clickNum=0;
				}
				//保留2位有效数字
				var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
				var sumAdpay=parseFloat(beans[j].sumAdpay).toFixed(2);
				var adPay=parseFloat(beans[j].adPay).toFixed(2);
				var proportion=parseFloat((beans[j].proportion)*100).toFixed(2);
				var device="PC";		//跳转到单日页面设备中英文切换
				var tbodys = "<tr><td>"+(j+1)+"</td>" +
				"<td class='area'>"+beans[j].systemType+"</td>" +
				"<td class='adNum'>"+beans[j].adNum+"</td>" +
				"<td class='aPrice'>"+(beans[j].averagePrice*1).toFixed(2)+"</td>" +
				"<td class='clickNum'>"+beans[j].allPay+"</td>" +
				"<td class='adIncome'>"+(beans[j].adPay*1).toFixed(2)+"</td>" +
				"<td class='allIncome'>"+(beans[j].proportion*1).toFixed(2)+"%"+"</td>" +
				"<td class='percent'>"+beans[j].adBudget+"</td>" +
				"<td class='rownum'>"+(j+1)+"</td>" +
				"<td><a href='/web-adv/advWeb/views/bigData/totalBySystemOneday.html?endTime="+beans[j].time+"&startTime="+beans[j].time+"&systemType="+device+"&page=1&selectedNav=4&amp&navPos=2&status=1"+"' class='btn button blue-btn day-btn'>按单日查看</a></td></tr>";
				$("#tb").append(tbodys);
				sumAdpayNum+=parseFloat(beans[j].clickNum);
				adPayNum+=parseFloat(beans[j].adPay);
				proportionNum+=parseFloat(beans[j].proportion);
				budgetMoney+=parseInt(beans[j].adBudget);
			}
			var tbodys = "<tr><td>总计</td>" +
					"<td>---</td>" +
					"<td class='adNum'>---</td>" +
					"<td class='aPrice'>---</td>" +
					"<td class='clickNum'>"+sumAdpayNum+"</td>" +
					"<td class='adIncome'>"+adPayNum.toFixed(2)+"</td>" +
					"<td class='allIncome'>"+proportionNum.toFixed(2)+"%"+"</td>" +
					"<td class='totaladvertisibudget'>"+budgetMoney+"</td>" +
					"<td>---</td>" +
					"<td>---</td></tr>";
			$("#tb").append(tbodys);
		}
	});
};
function bySystem(page) {
	$('tbody > tr td').remove();
	if(page==null || page.length<=0){
		var page=1;
	}
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var systemType = $("#systemType").val();
	var param = {
		"startTime" : startTime,
		"endTime" : endTime,
		"systemType" : systemType,
		"page" : page
	}
	$.ajax({
		url : '/web-adv/bySystem',
		data : param,
		type : 'post',
		success : function(data) {
			console.log(data);
			$('tbody > tr td').remove();
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	getPage(page);	
	            }
			});
			$("#endTime").val(data.endTime);
			$("#startTime").val(data.startTime);
			var beans = data.bigData;
			var sumAdpayNum=0;
			var budgetMoney=0;
			var adPayNum=0;
			var proportionNum=0;
			for(var j = 0; j < beans.length;j++){
				if(typeof beans[j].clickNum=="object"){
					beans[j].clickNum=0;
				};
				var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
				var adPay=parseFloat(beans[j].adPay).toFixed(2);
				var proportion=parseFloat((beans[j].proportion)*100).toFixed(2);
				var adBudget=parseFloat(beans[j].adBudget).toFixed(2);
				var click = beans[j].clickNum==null?0:beans[j].clickNum
				var device="PC";		//跳转到单日页面设备中英文切换
				var tbodys = "<tr><td>"+(j+1)+"</td>" +
				"<td class='area'>"+beans[j].systemType+"</td>" +
				"<td class='adNum'>"+beans[j].adNum+"</td>" +
				"<td class='aPrice'>"+(beans[j].averagePrice*1).toFixed(2)+"</td>" +
				"<td class='clickNum'>"+beans[j].allPay+"</td>" +
				"<td class='adIncome'>"+(beans[j].adPay*1).toFixed(2)+"</td>" +
				"<td class='allIncome'>"+(beans[j].proportion*1).toFixed(2)+"%"+"</td>" +
				"<td class='percent'>"+beans[j].adBudget+"</td>" +
				"<td class='rownum'>"+(j+1)+"</td>" +
				"<td><a href='/web-adv/advWeb/views/bigData/totalBySystemOneday.html?endTime="+beans[j].time+"&startTime="+beans[j].time+"&systemType="+device+"&page=1&selectedNav=4&amp&navPos=2&status=1"+"' class='btn button blue-btn day-btn'>按单日查看</a></td></tr>";
				$("#tb").append(tbodys);
				sumAdpayNum+=parseFloat(beans[j].clickNum);
				adPayNum+=parseFloat(beans[j].adPay);
				proportionNum+=parseFloat(beans[j].proportion);
				budgetMoney+=parseInt(beans[j].adBudget);
			}
			if(data.pageNum==data.pages&&data.bigData.length!=0){
				var aprice = ((data.totalIncome/data.allIncome)*100).toFixed(2)+"%";
				var tbodys = "<tr><td>总计</td>" +
						"<td>---</td>" +
						"<td class='adNum'>---</td>" +
						"<td class='aPrice'>---</td>" +
						"<td class='clickNum'>"+sumAdpayNum+"</td>" +
						"<td class='adIncome'>"+adPayNum.toFixed(2)+"</td>" +
						"<td class='allIncome'>"+proportionNum.toFixed(2)+"%"+"</td>" +
						"<td class='totaladvertisibudget'>"+budgetMoney+"</td>" +
						"<td>---</td>" +
						"<td>---</td></tr>";
				$("#tb").append(tbodys);
			}
			$("#page").show();
		}
	});
};




